//
//  DHLoginView.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/8.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHLoginView.h"
@interface DHLoginView()
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

@end

@implementation DHLoginView

+ (instancetype)loginViewWithFrame:(CGRect)frame
{
    UINib *nib = [UINib nibWithNibName:@"DHLoginView" bundle:nil];
    
    DHLoginView *view = [nib instantiateWithOwner:nil options:nil][0];
    view.frame = frame;
    return view;
}

- (void)setUserName:(NSString *)userName{
    _userName = userName;
    
    self.userNameTextField.text = userName;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 13, 21)];
    label.backgroundColor = [UIColor clearColor];
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 13, 21)];
    label1.backgroundColor = [UIColor clearColor];
    self.userNameTextField.leftViewMode = UITextFieldViewModeAlways;
    self.pwdTextField.leftViewMode = UITextFieldViewModeAlways;
    
    self.userNameTextField.leftView = label;
    self.pwdTextField.leftView = label1;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}

#pragma mark: - 关闭按钮
- (IBAction)closeButtonClick:(UIButton *)sender
{
    [self endEditing:YES];
  
    NSAssert(self.noneLoginCloseViewBlock != nil, @"self.noneLoginCloseViewBlock 不能为nil");

    self.noneLoginCloseViewBlock(false);
}

#pragma mark: - 注册按钮监听
- (IBAction)registButtonClick:(id)sender
{
    NSAssert(self.registButtonClickBlock != nil, @"self.registButtonClickBlock 不能为nil");
    self.registButtonClickBlock();
}

#pragma mark: - 忘记密码按钮监听
- (IBAction)forgetPwdBtnClick:(UIButton *)sender
{
 
    NSAssert(self.forgetPwdBlock != nil, @"forgetPwdBlock 不能为nil");
    self.forgetPwdBlock();
}


#pragma mark: - 登录按钮监听
- (IBAction)loginButtonClick:(id)sender
{
    if ([self.userNameTextField.text isEqualToString:@""] ||
        [self.pwdTextField.text isEqualToString:@""]) {
        return;
    }
    
    [DHNetworkManager.shareManager userLogin:self.userNameTextField.text
                                    password:self.pwdTextField.text
                                  completion:^(BOOL isSuccess, NSString *statusInfo) {
        
        if (!isSuccess) {
            if (![statusInfo isEqualToString:@"用户名密码错误"]){
                [SVProgressHUD showErrorWithStatus:@"登录失败"];
                return ;
            }
            [SVProgressHUD showErrorWithStatus:statusInfo];
            return;
        }
           
        [SVProgressHUD showSuccessWithStatus:@"登录成功"];
                                      
        NSAssert(self.noneLoginCloseViewBlock != nil, @"self.noneLoginCloseViewBlock 不能为nil");
                                      
        self.noneLoginCloseViewBlock(true);
    }];

}

#pragma mark: - 第三方登录
- (IBAction)wechatButtonClick:(id)sender {
    //FIXME: - 未实现

}
- (IBAction)qqButtonClick:(id)sender {
    //FIXME: - 未实现

}
- (IBAction)sinaButtonClick:(id)sender {
    //FIXME: - 未实现

}
@end
